﻿using System;
//老亚瑟
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace Git.Storage.WebApi.Common
{
    /// <summary>
    /// 
    /// </summary>
    public class ProcHelpers
    {
        /// <summary>
        /// 公共方法
        /// </summary>
        /// <param name="procedureName"></param>
        /// <param name="parameters"></param>
        /// <param name="outputValue"></param>
        public void ExecuteStoredProcedureWithOutput(string procedureName, SqlParameter[] parameters, out string outputValue)
        {
            using (SqlConnection connection = new SqlConnection("Data Source=10.40.92.31;Initial Catalog=JooWMS;User ID=sa;Password=1qaz@WSX;"))
            {
                connection.Open();

                using (SqlCommand command = new SqlCommand(procedureName, connection))
                {
                    command.CommandType = CommandType.StoredProcedure;

                    if (parameters != null)
                    {
                        foreach (SqlParameter parameter in parameters)
                        {
                            command.Parameters.Add(parameter);
                        }
                    }

                    // 添加输出参数并设置方向
                    SqlParameter outputParameter = new SqlParameter("@ReturnValue", SqlDbType.VarChar)
                    {
                        Direction = ParameterDirection.Output,
                        Size = 50 // 设置输出参数的大小（字符串类型）
                    };
                    command.Parameters.Add(outputParameter);

                    command.ExecuteNonQuery();

                    // 获取输出参数的值
                    outputValue = outputParameter.Value.ToString();
                }
            }
        }
    }
}